﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using FacePassword.Face_Recognition;


namespace FacePassword.UI
{
    public partial class DetectFaceForm : Form
    {
        public Capture _capture = null;

        private bool _captureInProgress;

        public WeberFR wF = new WeberFR();

       // TrainUser form = new TrainUser();

        Bitmap picture;

        public DetectFaceForm()
        {
            try
            {
                _capture = new Capture();
                _capture.ImageGrabbed += ProcessFrame;
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }

            InitializeComponent();

        }
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
            
            picture = frame.Bitmap;
            
            captureImageBox.Image = frame;

          /*  if (frame != null)
            {
                MessageBox.Show("The Picture is taken.");
                DetectFaceForm form = new DetectFaceForm();
                this.Close();
                form.Show();
            }
            else
                MessageBox.Show("Face isn't found");*/
        }

        
        
        
        private void captureButton_Click(object sender, EventArgs e)
        {

            if (_capture != null)
            {
                if (_captureInProgress)
                {  //stop the capture
                    captureButton.Text = "Start Capture";
                    _capture.Pause();
                }
                else
                {
                    //start the capture
                    captureButton.Text = "Stop";
                    _capture.Start();
                    
                }
                     
                _captureInProgress = !_captureInProgress;
                    
            }
          //  Image image = wF.ExtractFace(3000);
           /* if (image != null)
            {
                MessageBox.Show("Picture is taken");

                TrainUser form = new TrainUser();
                form.Show();
            }

            else
                MessageBox.Show("Face isn't found");*/
        }

        private void ReleaseData()
        {
            if (_capture != null)
                _capture.Dispose();
        }

        private void FinishCapturing_Click(object sender, EventArgs e)
        {
            TrainUser.ActiveForm.Activate();
            
            this.Close();

          
            //form.Show();
        }


        public Bitmap PutFrameToImgBoxToOtherForm()
        {
            return picture;
        }
        
       
        
    }
}
